<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/dxl/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/dxl/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/dxl/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/dxl/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/dxl/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/dxl/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/dxl/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/dxl/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/dxl/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="数据库,">










<meta name="description" content="一、介绍​        Django自带的ORM框架,也正是因为是Django原生的,所以兼容性远远不如SQLAlchemy ​        SQLAlchemy ORM框架,它可以在任何使用SQL查询时使用 二、创建数据表12345678910111213141516171819from  sqlalchemy import Columnfrom sqlalchemy import INT,">
<meta name="keywords" content="数据库">
<meta property="og:type" content="article">
<meta property="og:title" content="SQLAlchemy的使用">
<meta property="og:url" content="http://yoursite.com/2019/09/18/【数据库】SQLALchemy的使用/index.html">
<meta property="og:site_name" content="我的快乐时光">
<meta property="og:description" content="一、介绍​        Django自带的ORM框架,也正是因为是Django原生的,所以兼容性远远不如SQLAlchemy ​        SQLAlchemy ORM框架,它可以在任何使用SQL查询时使用 二、创建数据表12345678910111213141516171819from  sqlalchemy import Columnfrom sqlalchemy import INT,">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-09-18T14:08:58.485Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="SQLAlchemy的使用">
<meta name="twitter:description" content="一、介绍​        Django自带的ORM框架,也正是因为是Django原生的,所以兼容性远远不如SQLAlchemy ​        SQLAlchemy ORM框架,它可以在任何使用SQL查询时使用 二、创建数据表12345678910111213141516171819from  sqlalchemy import Columnfrom sqlalchemy import INT,">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/dxl/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/09/18/【数据库】SQLALchemy的使用/">





  <title>SQLAlchemy的使用 | 我的快乐时光</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/dxl/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">我的快乐时光</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/dxl/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/dxl/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/dxl/2019/09/18/【数据库】SQLALchemy的使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content>
      <meta itemprop="description" content>
      <meta itemprop="image" content="/dxl/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="我的快乐时光">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">SQLAlchemy的使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-18T21:10:45+08:00">
                2019-09-18
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/dxl/categories/数据库/" itemprop="url" rel="index">
                    <span itemprop="name">数据库</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/dxl/2019/09/18/【数据库】SQLALchemy的使用/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/dxl/2019/09/18/【数据库】SQLALchemy的使用/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="一、介绍"><a href="#一、介绍" class="headerlink" title="一、介绍"></a>一、介绍</h2><p>​        Django自带的ORM框架,也正是因为是Django原生的,所以兼容性远远不如SQLAlchemy</p>
<p>​        SQLAlchemy ORM框架,它可以在任何使用SQL查询时使用</p>
<h2 id="二、创建数据表"><a href="#二、创建数据表" class="headerlink" title="二、创建数据表"></a>二、创建数据表</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span>  sqlalchemy <span class="keyword">import</span> Column</span><br><span class="line"><span class="keyword">from</span> sqlalchemy <span class="keyword">import</span> INT,Integer,INTEGER</span><br><span class="line"><span class="keyword">from</span> sqlalchemy <span class="keyword">import</span>  CHAR,VARCHAR,String</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> sqlalchemy.ext.declarative <span class="keyword">import</span> declarative_base</span><br><span class="line"><span class="comment"># 1、实例化模型 -model就是 ORM 模型</span></span><br><span class="line">Model =declarative_base()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2、创建表</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Users</span><span class="params">(Model)</span>:</span></span><br><span class="line">    __tablename__ = <span class="string">"users"</span> <span class="comment">#为table创建名称</span></span><br><span class="line">    id = Column(Integer,primary_key=<span class="literal">True</span>,autoincrement=<span class="literal">True</span>)</span><br><span class="line">    name = Column(String(<span class="number">32</span>),nullable=<span class="literal">False</span>,unique= <span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3、去连接数据库 创建数据引擎</span></span><br><span class="line"><span class="keyword">from</span> sqlalchemy <span class="keyword">import</span> create_engine</span><br><span class="line">engine = create_engine(<span class="string">"mysql+pymysql://root:123@127.0.0.1:3306/dxl?charset=utf8"</span>)</span><br><span class="line"><span class="comment"># 4、Model 自动检索所有继承Model的ORM 对象 并且创建所有的数据表</span></span><br><span class="line">Model.metadata.create_all(engine)</span><br></pre></td></tr></table></figure>

<h2 id="三、数据库的增删改查操作"><a href="#三、数据库的增删改查操作" class="headerlink" title="三、数据库的增删改查操作"></a>三、数据库的增删改查操作</h2><h3 id="3-1增加数据"><a href="#3-1增加数据" class="headerlink" title="3.1增加数据"></a>3.1增加数据</h3><p>1、插入一条数据，带解释</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">from sqlalchemy.orm import sessionmaker</span><br><span class="line">from s1 import  engine #导入之前创建好的 create_engine</span><br><span class="line">from s1 import Users  #首先导入之间做好的ORM 对象 User</span><br><span class="line">user1 = Users(name=&quot;李白&quot;) #使用Users ORM模型创建一条数据</span><br><span class="line">#创建 sessionmaker 会话对象,将数据库引擎 engine 交给 sessionmaker</span><br><span class="line">Session = sessionmaker(engine)</span><br><span class="line"># 打开会话对象 Session</span><br><span class="line">db_session =Session()</span><br><span class="line">#在db_session会话中添加一条 UserORM模型创建的数据</span><br><span class="line">db_session.add(user1)</span><br><span class="line">#使用 db_session 会话提交 , 这里的提交是指将db_session中的所有指令一次性提交</span><br><span class="line">db_session.commit()</span><br><span class="line">#关闭会话</span><br><span class="line">db_session.close()</span><br></pre></td></tr></table></figure>

<p>2、插入多条数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#方法一</span></span><br><span class="line">user1 = Users(name=<span class="string">"小白"</span>)</span><br><span class="line">user2 = Users(name=<span class="string">"小黑"</span>)</span><br><span class="line">Session = sessionmaker(engine)</span><br><span class="line">db_session =Session()</span><br><span class="line">db_session.add(user1)</span><br><span class="line">db_session.add(user2)</span><br><span class="line">db_session.commit()</span><br><span class="line">db_session.close()</span><br><span class="line"><span class="comment">#方法二</span></span><br><span class="line">user_list=[Users(name=<span class="string">"欣欣同学"</span>),Users(name=<span class="string">"小铭同学"</span>)]</span><br><span class="line">Session = sessionmaker(engine)</span><br><span class="line">db_session =Session()</span><br><span class="line">db_session.add_all(user_list) <span class="comment">#单条数据添加用add ，多条用add_all</span></span><br><span class="line">db_session.commit()</span><br><span class="line">db_session.close()：</span><br></pre></td></tr></table></figure>

<h3 id="3-2查询数据"><a href="#3-2查询数据" class="headerlink" title="3.2查询数据"></a>3.2查询数据</h3><p>1、查询所有数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Session = sessionmaker(engine)</span><br><span class="line">db_session =Session()</span><br><span class="line">info = db_session.query(Users).all()</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> info:</span><br><span class="line">    print(i.id,i.name)</span><br><span class="line"></span><br><span class="line">db_session.commit()</span><br><span class="line">db_session.close()</span><br></pre></td></tr></table></figure>

<p>2、查询id 大于2的数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Session = sessionmaker(engine)</span><br><span class="line">db_session =Session()</span><br><span class="line">info = db_session.query(Users).filter(Users.id &gt;2).all()</span><br><span class="line">for i in info:</span><br><span class="line">    print(i.id,i.name)</span><br><span class="line"></span><br><span class="line">db_session.commit()</span><br><span class="line">db_session.close()</span><br></pre></td></tr></table></figure>

<p>3、first 取第一条数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Session = sessionmaker(engine)</span><br><span class="line">db_session =Session()</span><br><span class="line">info = db_session.query(Users).filter(Users.id &gt;2).first()</span><br><span class="line">print(info.id,info.name)</span><br><span class="line">db_session.commit()</span><br><span class="line">db_session.close()</span><br></pre></td></tr></table></figure>

<p>4、不取数据的情况</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Session = sessionmaker(engine)</span><br><span class="line">db_session =Session()</span><br><span class="line">info = db_session.query(Users).filter(Users.id &gt;<span class="number">2</span>)</span><br><span class="line"><span class="comment"># print(info.id,info.name)</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">SELECT users.id AS users_id, users.name AS users_name FROM users WHERE users.id &gt; %(id_1)s</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line">print(info)</span><br><span class="line">db_session.commit()</span><br><span class="line">db_session.close()</span><br></pre></td></tr></table></figure>

<h3 id="3-3修改数据"><a href="#3-3修改数据" class="headerlink" title="3.3修改数据"></a>3.3修改数据</h3><p>1、更改一行数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Session = sessionmaker(engine)</span><br><span class="line">db_session =Session()</span><br><span class="line">info = db_session.query(Users).filter(Users.id ==2).update(&#123;&quot;name&quot;:&quot;小李&quot;&#125;)</span><br><span class="line"># print(info.id,info.name)</span><br><span class="line">print(info) #1 当前这句更新语句所更新的行数</span><br><span class="line">db_session.commit()</span><br><span class="line">db_session.close()</span><br></pre></td></tr></table></figure>

<p>2、更改多行数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Session = sessionmaker(engine)</span><br><span class="line">db_session =Session()</span><br><span class="line">info = db_session.query(Users).filter(Users.id &lt;<span class="number">3</span>).update(&#123;<span class="string">"name"</span>:<span class="string">"天霸"</span>&#125;)<span class="comment">#字段设置了unique就不可以这样修改</span></span><br><span class="line"><span class="comment"># print(info.id,info.name)</span></span><br><span class="line">print(info) <span class="comment">#1 当前这句更新语句所更新的行数</span></span><br><span class="line">db_session.commit()</span><br><span class="line">db_session.close()</span><br></pre></td></tr></table></figure>

<h3 id="3-4删除数据"><a href="#3-4删除数据" class="headerlink" title="3.4删除数据"></a>3.4删除数据</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Session = sessionmaker(engine)</span><br><span class="line">db_session =Session()</span><br><span class="line">info = db_session.query(Users).filter(Users.id ==<span class="number">3</span>).delete()</span><br><span class="line"></span><br><span class="line">print(info) <span class="comment">#1 当前这句更新语句所更新的行数</span></span><br><span class="line">db_session.commit()</span><br><span class="line">db_session.close()</span><br></pre></td></tr></table></figure>

<h3 id="3-5高级版查询操作"><a href="#3-5高级版查询操作" class="headerlink" title="3.5高级版查询操作"></a>3.5高级版查询操作</h3><p>1、and的查询</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sqlalchemy.sql <span class="keyword">import</span> and_</span><br><span class="line"><span class="comment"># info = db_session.query(Users).filter(Users.id ==2,Users.name=="小李").all()</span></span><br><span class="line">info = db_session.query(Users).filter(and_(Users.id ==<span class="number">2</span>,Users.name==<span class="string">"小李"</span>)).all()</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> info:</span><br><span class="line">    print(i.id,i.name)</span><br></pre></td></tr></table></figure>

<p>2、or</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sqlalchemy.sql <span class="keyword">import</span> or_</span><br><span class="line">info = db_session.query(Users).filter(or_(Users.id ==<span class="number">2</span>,Users.name==<span class="string">"李白"</span>)).all()</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> info:</span><br><span class="line">    print(i.id,i.name)</span><br><span class="line"><span class="comment">#or 与and的混用</span></span><br><span class="line">info = db_session.query(Users).filter(or_(Users.id ==<span class="number">2</span>,and_(Users.name==<span class="string">"李白"</span>,Users.id==<span class="number">2</span>))).all()</span><br></pre></td></tr></table></figure>

<p>3、查询数据 指定查询数据列 加入别名</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users.name.label(<span class="string">"user_name"</span>),Users.id).filter(or_(Users.id ==<span class="number">2</span>,Users.name==<span class="string">"李白"</span>)).all()</span><br><span class="line"><span class="comment"># print(info)</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> info:</span><br><span class="line">    print(i.id,i.user_name)</span><br></pre></td></tr></table></figure>

<p>4、原生sql查询</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users.name.label(&quot;user_name&quot;),Users.id).filter_by(name=&quot;欣欣同学&quot;).all()</span><br></pre></td></tr></table></figure>

<p>5、order_by</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users).filter(Users.id&lt;<span class="number">3</span>).order_by(Users.id).all()</span><br><span class="line">info = db_session.query(Users).filter(Users.id&lt;<span class="number">3</span>).order_by(Users.id.desc()).all()</span><br><span class="line">info = db_session.query(Users).filter(Users.id&lt;<span class="number">3</span>).order_by(Users.id.asc()).all()</span><br><span class="line">info=db_session.query(Users).filter(Users.id&lt;<span class="number">3</span>).order_by(Users.id.asc(),Users.name.asc()).all()</span><br></pre></td></tr></table></figure>

<p>6、params（传入参数）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users).filter(text(&quot;id&lt;:value and name=:name&quot;)).params(value=2, name=&apos;欣欣同学&apos;).order_by( Users.id)</span><br></pre></td></tr></table></figure>

<p>7、原生sql</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users).filter(text(&quot;id&lt;:value and name=:name&quot;)).params(value=5, name=&apos;欣欣同学&apos;).order_by(Users.id)</span><br></pre></td></tr></table></figure>

<p>8、筛选查询列</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">user_list = db_session.query(Users.name).all()</span><br><span class="line"> 38 print(user_list)</span><br><span class="line"> 39 for row in user_list:</span><br><span class="line"> 40     print(row.name)</span><br></pre></td></tr></table></figure>

<p>9、别名映射  name as nick</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users.name.label(&quot;nick&quot;)).all()</span><br><span class="line">for i in info :</span><br><span class="line">    print(i.nick)</span><br></pre></td></tr></table></figure>

<p>10、between</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users).filter(Users.id.between(1,3)).all()</span><br><span class="line">for i in info:</span><br><span class="line">    print(i.id,i.name)</span><br></pre></td></tr></table></figure>

<p>11、in、~</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users).filter(Users.id.in_([1,3])).all()</span><br><span class="line">for i in info:</span><br><span class="line">    print(i.id,i.name)  </span><br><span class="line"> info = db_session.query(Users).filter(~Users.id.in_([1,3])).all()</span><br></pre></td></tr></table></figure>

<p>12、子查询</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">info=db_session.query(Users).filter(Users.id.in_(db_session.query(Users.id).filter(Users.name==&quot;欣欣同学&quot;))).all()</span><br></pre></td></tr></table></figure>

<p>13、通配符</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users).filter(Users.name.like(&quot;小%&quot;))</span><br></pre></td></tr></table></figure>

<p>14、限制</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users)[0:3]</span><br></pre></td></tr></table></figure>

<p>15、分组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users).group_by(Users.dep).all()</span><br><span class="line"><span class="comment">#取出的结果是每个分组的第一条数据</span></span><br><span class="line">info = db_session.query(func.max(Users.id)).group_by(Users.dep).all()<span class="comment">#可以这样写</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">结果：[(2,), (5,)]</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line">info = db_session.query(func.max(Users.id),Users.name).group_by(Users.dep).all()</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">结果：[(2, '李白'), (5, '欣欣同学')]有误</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line">info = db_session.query(func.max(Users.id),func.min(Users.id)).group_by(Users.dep).having(func.min(Users.id)&gt;<span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3-6高级修改数据操作"><a href="#3-6高级修改数据操作" class="headerlink" title="3.6高级修改数据操作"></a>3.6高级修改数据操作</h3><p>1、给数值批量+”100”</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users).filter(Users.id &lt;<span class="number">3</span>).update(&#123;Users.name:Users.name+<span class="string">"100"</span>&#125;,synchronize_session=<span class="literal">False</span>)</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">synchronize_session=False</span></span><br><span class="line"><span class="string">不仅可以对字符串进行操作，也可以对int数字进行操作</span></span><br><span class="line"><span class="string">"""</span></span><br></pre></td></tr></table></figure>

<p>2、给数值批量+50</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">info = db_session.query(Users).filter(Users.id &lt;<span class="number">3</span>).update(&#123;Users.dep:Users.dep<span class="number">-3</span>&#125;,synchronize_session=<span class="string">"evaluate"</span>)</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">synchronize_session=False</span></span><br><span class="line"><span class="string">只可以对int数字进行操作</span></span><br><span class="line"><span class="string">"""</span></span><br></pre></td></tr></table></figure>

<h2 id="四、一对多的操作"><a href="#四、一对多的操作" class="headerlink" title="四、一对多的操作"></a>四、一对多的操作</h2><h3 id="4-1创建相关表"><a href="#4-1创建相关表" class="headerlink" title="4.1创建相关表"></a>4.1创建相关表</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">s3.py</span><br><span class="line"><span class="keyword">from</span> sqlalchemy.ext.declarative <span class="keyword">import</span> declarative_base</span><br><span class="line"><span class="keyword">from</span> sqlalchemy <span class="keyword">import</span>  Integer,Column,String,ForeignKey</span><br><span class="line"><span class="keyword">from</span> sqlalchemy.orm <span class="keyword">import</span> relationship</span><br><span class="line">Model = declarative_base()</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Classes</span><span class="params">(Model)</span>:</span></span><br><span class="line">    __tablename__ = <span class="string">"class"</span></span><br><span class="line">    id = Column(Integer,primary_key=<span class="literal">True</span>)</span><br><span class="line">    name = Column(String(<span class="number">32</span>))</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Students</span><span class="params">(Model)</span>:</span></span><br><span class="line">    __tablename__ = <span class="string">"students"</span></span><br><span class="line">    id = Column(Integer,primary_key=<span class="literal">True</span>)</span><br><span class="line">    name = Column(String(<span class="number">32</span>))</span><br><span class="line">    class_id = Column(Integer,ForeignKey(<span class="string">"class.id"</span>))</span><br><span class="line">    to_class = relationship(<span class="string">"Classes"</span>,backref =<span class="string">"stuclass"</span>)</span><br><span class="line"><span class="keyword">from</span> sqlalchemy <span class="keyword">import</span> create_engine</span><br><span class="line">engine = create_engine(<span class="string">"mysql+pymysql://root:123@127.0.0.1:3306/dxl?charset=utf8"</span>)</span><br><span class="line">Model.metadata.create_all(engine)</span><br></pre></td></tr></table></figure>

<h3 id="4-2增加数据"><a href="#4-2增加数据" class="headerlink" title="4.2增加数据"></a>4.2增加数据</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">s4.py</span><br><span class="line">from s3 import Students, Classes, engine</span><br><span class="line">from sqlalchemy.orm import sessionmaker</span><br><span class="line"></span><br><span class="line">DB_session = sessionmaker(engine)</span><br><span class="line">db_session = DB_session()</span><br></pre></td></tr></table></figure>

<p>1、方式一</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、添加班级</span><br><span class="line">db_session.add_all([Classes(name=<span class="string">"飞天"</span>),Classes(name=<span class="string">"落地"</span>)])</span><br><span class="line"><span class="number">2</span>、添加学生</span><br><span class="line">class_obj =db_session.query(Classes).filter(Classes.name==<span class="string">"落地"</span>).first()</span><br><span class="line">stu =Students(name =<span class="string">"小铭同学"</span>,class_id=class_obj.id)</span><br><span class="line">db_session.add(stu)</span><br></pre></td></tr></table></figure>

<p>2、方式二 relationship</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">to_class 做了哪两件事：</span><br><span class="line">第一件事 在ClassTable表中添加一条数据</span><br><span class="line">第二件事 在Student表中添加一条数据并将刚刚添加的ClassTable的数据id填写在Student的class_id中</span><br><span class="line">正向添加数据</span><br><span class="line">stu = Students(name =<span class="string">"嗨嗨同学"</span>,to_class=Classes(name=<span class="string">"航空"</span>))</span><br><span class="line">反向添加数据</span><br><span class="line">class_obj = db_session.query(Classes).filter(Classes.name==<span class="string">"飞天"</span>).first()</span><br><span class="line">class_obj.stuclass=[Students(name=<span class="string">"依依"</span>),Students(name=<span class="string">"乐乐"</span>)]</span><br></pre></td></tr></table></figure>

<h3 id="4-3查询数据"><a href="#4-3查询数据" class="headerlink" title="4.3查询数据"></a>4.3查询数据</h3><p>1、查询所有数据并显示班级名称</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#正向查询</span></span><br><span class="line">stus =db_session.query(Students).all()</span><br><span class="line"><span class="keyword">for</span> row <span class="keyword">in</span> stus:</span><br><span class="line">    print(row.name,row.to_class.name,row.class_id)</span><br><span class="line"><span class="comment">#方向查询</span></span><br><span class="line">class_list =db_session.query(Classes).all()</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> class_list:</span><br><span class="line">    <span class="keyword">for</span> row <span class="keyword">in</span> i.stuclass:</span><br><span class="line">        print(i.name,row.name)</span><br></pre></td></tr></table></figure>

<h3 id="4-4更新数据"><a href="#4-4更新数据" class="headerlink" title="4.4更新数据"></a>4.4更新数据</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">classinfo = db_session.query(Classes).filter(Classes.name==&quot;飞天&quot;).first()</span><br><span class="line">db_session.query(Students).filter(Students.class_id ==classinfo.id).update(&#123;&quot;name&quot;:&quot;高翔&quot;&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="4-5删除数据"><a href="#4-5删除数据" class="headerlink" title="4.5删除数据"></a>4.5删除数据</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#删除某一班级的全部学生</span><br><span class="line">classinfo = db_session.query(Classes).filter(Classes.name==&quot;飞天&quot;).first()</span><br><span class="line">db_session.query(Students).filter(Students.class_id ==classinfo.id).delete()</span><br></pre></td></tr></table></figure>

<h2 id="五、多对多的操作"><a href="#五、多对多的操作" class="headerlink" title="五、多对多的操作"></a>五、多对多的操作</h2><h3 id="5-1、创建表及关系"><a href="#5-1、创建表及关系" class="headerlink" title="5.1、创建表及关系"></a>5.1、创建表及关系</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">from sqlalchemy.ext.declarative import declarative_base</span><br><span class="line">from sqlalchemy import Integer, Column, String, create_engine, ForeignKey</span><br><span class="line">from sqlalchemy.orm import relationship</span><br><span class="line"></span><br><span class="line">Model = declarative_base()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class Hotel(Model):</span><br><span class="line">    __tablename__ = &quot;hotel&quot;</span><br><span class="line">    id = Column(Integer, primary_key=True)</span><br><span class="line">    name = Column(String(32))</span><br><span class="line">    girl_id = Column(Integer, ForeignKey(&quot;girl.id&quot;))</span><br><span class="line">    boy_id = Column(Integer, ForeignKey(&quot;boy.id&quot;))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class Girl(Model):</span><br><span class="line">    __tablename__ = &quot;girl&quot;</span><br><span class="line">    id = Column(Integer, primary_key=True)</span><br><span class="line">    name = Column(String(32))</span><br><span class="line">    boys = relationship(&quot;Boy&quot;, secondary=&quot;hotel&quot;, backref=&quot;girl2boy&quot;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class Boy(Model):</span><br><span class="line">    __tablename__ = &quot;boy&quot;</span><br><span class="line">    id = Column(Integer, primary_key=True)</span><br><span class="line">    name = Column(String(32))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">engine = create_engine(&quot;mysql+pymysql://root:123@127.0.0.1:3306/dxl?charset=utf8&quot;)</span><br><span class="line"></span><br><span class="line">Model.metadata.create_all(engine)</span><br></pre></td></tr></table></figure>

<h3 id="5-2添加数据"><a href="#5-2添加数据" class="headerlink" title="5.2添加数据"></a>5.2添加数据</h3><p>基础数数的导入</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">from s5 import Boy,Girl,engine</span><br><span class="line">from sqlalchemy.orm import sessionmaker</span><br><span class="line">sessions = sessionmaker(engine)</span><br><span class="line">db_session = sessions()</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#反向添加数据</span></span><br><span class="line">boy =Boy(name=<span class="string">"一鸣"</span>)</span><br><span class="line">boy.girl2boy=[Girl(name=<span class="string">"赵李颖"</span>),Girl(name=<span class="string">"热巴"</span>)]</span><br><span class="line">db_session.add(boy)</span><br><span class="line"><span class="comment">#正向添加数据</span></span><br><span class="line">girl =Girl(name =<span class="string">"热轧"</span>)</span><br><span class="line">girl.boys=[Boy(name=<span class="string">"丽丽"</span>),Boy(name=<span class="string">"华华"</span>)]</span><br><span class="line">db_session.add(girl)</span><br></pre></td></tr></table></figure>

<h3 id="5-3查询数据"><a href="#5-3查询数据" class="headerlink" title="5.3查询数据"></a>5.3查询数据</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#方向查询</span></span><br><span class="line">hotel = db_session.query(Boy).all()</span><br><span class="line"><span class="keyword">for</span> row <span class="keyword">in</span> hotel:</span><br><span class="line">    <span class="keyword">for</span> row <span class="keyword">in</span> hotel:</span><br><span class="line">        <span class="keyword">for</span> row2 <span class="keyword">in</span> row.girl2boy:</span><br><span class="line">            print(row.name,row2.name)</span><br><span class="line"><span class="comment">#反向查询</span></span><br><span class="line">hotel = db_session.query(Girl).all()</span><br><span class="line"><span class="keyword">for</span> row <span class="keyword">in</span> hotel:</span><br><span class="line">    <span class="keyword">for</span> row <span class="keyword">in</span> hotel:</span><br><span class="line">        <span class="keyword">for</span> row2 <span class="keyword">in</span> row.boys:</span><br><span class="line">            print(row.name,row2.name)</span><br></pre></td></tr></table></figure>


      
    </div>
    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://yoursite.com/2019/09/18/【数据库】SQLALchemy的使用/" title="SQLAlchemy的使用">http://yoursite.com/2019/09/18/【数据库】SQLALchemy的使用/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/dxl/tags/数据库/" rel="tag"># 数据库</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/dxl/2019/09/04/【go语言】01基本数据类型/" rel="next" title="888">
                <i class="fa fa-chevron-left"></i> 888
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/dxl/2019/09/18/【数据库mogodb】基本数据类型/" rel="prev" title="MongoDB基础数据类型">
                MongoDB基础数据类型 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/dxl/images/avatar.png" alt>
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/dxl/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/dxl/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/dxl/tags/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-sign-out"></i>
                我的友链
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="tencent://message/?Menu=yes&uin=1258517737&Site=QQ%E6%9E%81%E5%AE%A2&Service=300&sigT=45a1e5847943b64c6ff3990f8a9e644d2b31356cb0b4ac6b24663a3c8dd0f8aa12a595b1714f9d45/" title="申请坑位" target="_blank">申请坑位</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#一、介绍"><span class="nav-number">1.</span> <span class="nav-text">一、介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、创建数据表"><span class="nav-number">2.</span> <span class="nav-text">二、创建数据表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三、数据库的增删改查操作"><span class="nav-number">3.</span> <span class="nav-text">三、数据库的增删改查操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1增加数据"><span class="nav-number">3.1.</span> <span class="nav-text">3.1增加数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2查询数据"><span class="nav-number">3.2.</span> <span class="nav-text">3.2查询数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3修改数据"><span class="nav-number">3.3.</span> <span class="nav-text">3.3修改数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4删除数据"><span class="nav-number">3.4.</span> <span class="nav-text">3.4删除数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5高级版查询操作"><span class="nav-number">3.5.</span> <span class="nav-text">3.5高级版查询操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6高级修改数据操作"><span class="nav-number">3.6.</span> <span class="nav-text">3.6高级修改数据操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#四、一对多的操作"><span class="nav-number">4.</span> <span class="nav-text">四、一对多的操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1创建相关表"><span class="nav-number">4.1.</span> <span class="nav-text">4.1创建相关表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2增加数据"><span class="nav-number">4.2.</span> <span class="nav-text">4.2增加数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3查询数据"><span class="nav-number">4.3.</span> <span class="nav-text">4.3查询数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4更新数据"><span class="nav-number">4.4.</span> <span class="nav-text">4.4更新数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-5删除数据"><span class="nav-number">4.5.</span> <span class="nav-text">4.5删除数据</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#五、多对多的操作"><span class="nav-number">5.</span> <span class="nav-text">五、多对多的操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1、创建表及关系"><span class="nav-number">5.1.</span> <span class="nav-text">5.1、创建表及关系</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2添加数据"><span class="nav-number">5.2.</span> <span class="nav-text">5.2添加数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3查询数据"><span class="nav-number">5.3.</span> <span class="nav-text">5.3查询数据</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-hand-peace-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder"></span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      本站访客数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      本站总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/dxl/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/dxl/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/dxl/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/dxl/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/dxl/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/dxl/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '13B0JGDuA6ttduN8AQaR8CzF-gzGzoHsz',
        appKey: 'I13r9r5mVgq4jQYpYy6V4gW3',
        placeholder: '欢迎大佬指点~~~',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
